﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace YinyuSearch
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            browsetxtDatabase();
        }
        private void browsetxtDatabase()
        {
            try
            {
                openFileDialog1.InitialDirectory = Path.GetDirectoryName(txtDatabase.Text);
            }
            catch (Exception)
            {
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
            }

            switch (openFileDialog1.ShowDialog(this))
            {
                case DialogResult.OK:
                    txtDatabase.Text = openFileDialog1.FileName;
                    break;
                default:
                    break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ListItem l1 = new ListItem("1", "Temperatur der Komponente  zu hoch");
            comboBox1.Items.Add(l1);
            ListItem l2 = new ListItem("2", "Arbeitsplatz zu eng");
            comboBox1.Items.Add(l2);
            ListItem l3 = new ListItem("3", "Werkzeug nicht entspricht");
            comboBox1.Items.Add(l3);
            ListItem l4 = new ListItem("4", "Ein Baustein muß erst demontieren");
            comboBox1.Items.Add(l4);

            if(txtDatabase.Text!="")
            {
                if(txtKeyword.Text!="")
                {
                    //string ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtDatabase.Text + ";Persist Security Info=False";

                    string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtDatabase.Text + ";Persist Security Info=False";
                    OleDbConnection Conn = new OleDbConnection(ConnStr);
                    string keyword = txtKeyword.Text;
                    OleDbCommand cmd = new OleDbCommand();
                    cmd.Connection = Conn;
                    Conn.Open();

                    //找出零件
                    cmd.CommandText = "select comid,ismodule,PhysicalComponentName,Coordinate_X1,Coordinate_X2,Coordinate_Y1,Coordinate_Y2,Coordinate_Z1,Coordinate_Z2,ispartof from yy_searchindex where PhysicalComponentName like '%" + keyword + "%'";
                    OleDbDataReader dr = cmd.ExecuteReader();
                    dr.Read();
                    PhysicalComponent p=new PhysicalComponent();
                    p.GUID = dr[0].ToString();
                    p.PhysicalComponentName = dr[2].ToString();
                    p.isPartof = dr[9].ToString();
                    p.Coordinate_X1 = double.Parse(dr[3].ToString());
                    p.Coordinate_X2 = double.Parse(dr[4].ToString());
                    p.Coordinate_Y1 = double.Parse(dr[5].ToString());
                    p.Coordinate_Y2 = double.Parse(dr[6].ToString());
                    p.Coordinate_Z1 = double.Parse(dr[7].ToString());
                    p.Coordinate_Z2 = double.Parse(dr[8].ToString());
                    
                    dr.Close();
                    //找出零件的遮挡关系
                    string s1 = "";
                    //X1方向
                    cmd.CommandText = "select * from yy_searchindex where Coordinate_X1>=" + p.Coordinate_X2;
                    cmd.CommandText = cmd.CommandText + " order by ismodule desc";
                    
                    OleDbDataReader dr1 = cmd.ExecuteReader();
                    
                    while (dr1.Read())
                    { 
                        s1 = s1 + dr1["PhysicalComponentName"].ToString() + "\n";
                        ListItem li = new ListItem(dr1["id"].ToString(), dr1["PhysicalComponentName"].ToString());
                        listBox1.Items.Add(li);
                    }
                    dr1.Close();
                    txtX1.Text = s1;

                    //X2方向
                    cmd.CommandText = "select * from yy_searchindex where Coordinate_X2<=" + p.Coordinate_X1;
                    cmd.CommandText = cmd.CommandText + " order by ismodule desc";

                    OleDbDataReader dr2 = cmd.ExecuteReader();
                    s1 = "";
                    while (dr2.Read())
                    {
                        s1 = s1 + dr2["PhysicalComponentName"].ToString() + "\n";
                    }
                    dr2.Close();
                    txtX2.Text = s1;

                    //Y1方向
                    cmd.CommandText = "select * from yy_searchindex where Coordinate_Y1>=" + p.Coordinate_Y2;
                    cmd.CommandText = cmd.CommandText + " order by ismodule desc";

                    OleDbDataReader dr3 = cmd.ExecuteReader();
                    s1 = "";
                    while (dr3.Read())
                    {
                        s1 = s1 + dr3["PhysicalComponentName"].ToString() + "\n";
                    }
                    dr3.Close();
                    txtY1.Text = s1;

                    //Y2方向
                    cmd.CommandText = "select * from yy_searchindex where Coordinate_Y2<=" + p.Coordinate_Y1;
                    cmd.CommandText = cmd.CommandText + " order by ismodule desc";

                    OleDbDataReader dr4 = cmd.ExecuteReader();
                    s1 = "";
                    while (dr4.Read())
                    {
                        s1 = s1 + dr4["PhysicalComponentName"].ToString() + "\n";
                    }
                    dr4.Close();
                    txtY2.Text = s1;

                    //Z1方向
                    cmd.CommandText = "select * from yy_searchindex where Coordinate_Z1>=" + p.Coordinate_Z2;
                    cmd.CommandText = cmd.CommandText + " order by ismodule desc";

                    OleDbDataReader dr5 = cmd.ExecuteReader();
                    s1 = "";
                    while (dr5.Read())
                    {
                        s1 = s1 + dr5["PhysicalComponentName"].ToString() + "\n";
                    }
                    dr5.Close();
                    txtZ1.Text = s1;

                    //Z2方向
                    cmd.CommandText = "select * from yy_searchindex where Coordinate_Z2<=" + p.Coordinate_Z1;
                    cmd.CommandText = cmd.CommandText + " order by ismodule desc";

                    OleDbDataReader dr6 = cmd.ExecuteReader();
                    s1 = "";
                    while (dr6.Read())
                    {
                        s1 = s1 + dr6["PhysicalComponentName"].ToString() + "\n";
                    }
                    dr6.Close();
                    txtZ2.Text = s1;
                    Conn.Close();
                }
                else
                {
                    MessageBox.Show("Please input key words.", "Error");
                }
            }
            else
            {
                MessageBox.Show("Please Select Database Path.", "Error");
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            switch (((ListItem)comboBox1.SelectedItem).ID)
            {
                case "1":
                    txtReason.Text = "Bitte checken Sie den Strom des Anlage, ob    sie abgeschaltet wird.\n";
                    txtReason.Text = txtReason.Text + "Bitte 5 Min. warten, um die Temperatur der Anlage abzusinken.\n";
                    txtReason.Text = txtReason.Text + "Bitte checken Sie die Temperatur der Umwelt.\n";
                    txtReason.Text = txtReason.Text + "";
                    break;
                case "2":
                    txtReason.Text = "Welche Komponente den hat Arbeitsplatz gesperrt?\n";
                    //txtReason.Text = txtReason.Text + "";
                    Main m=new Main();
                    m.Show();
                    break;
                case "3":
                    txtReason.Text = "Welche Komponente kann nicht demontiert werden?\n";

                    break;
                case "4":
                    //txtReason.Text = "Welche Komponente kann nicht demontiert werden?\n";
                    Main m2 = new Main();
                    m2.Show();
                    break; 
                    default:
                    break;
            }
        }

        private void ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtDatabase.Text + ";Persist Security Info=False";
            OleDbConnection Conn = new OleDbConnection(ConnStr);
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = Conn;
            cmd.CommandText = "Select * from yy_searchindex where id="+((ListItem)listBox1.SelectedItem).ID;
            Conn.Open();
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr["temp"].Equals(DBNull.Value))
                {
                    MessageBox.Show("No temperature limiting", "Message");
                }
                else
                {
                    MessageBox.Show("Temperature requirements below " + dr["temp"].ToString() + " degrees", "Message");
                }
            }
            dr.Close();
            Conn.Close();
        }
        public void reBind(int id)
        {
            listBox1.ClearSelected();
 
            for (int i = 0; i < listBox1.Items.Count;i++ )
            {
                if (((ListItem)listBox1.Items[i]).ID == id.ToString())
                {
                    listBox1.SetSelected(i, true);
                }
            }
        }

        private void ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            askDialog a = new askDialog();
            a.rebind = new askDialog.reBind(reBind);
            a.DbPath = txtDatabase.Text;
            a.ShowDialog();
        }

        private void ToolStripMenuItem4_Click(object sender, EventArgs e)
        {
            moveDialog m=new moveDialog();
            m.mp = new moveDialog.moveUp(moveUp);
            m.DbPath = txtDatabase.Text;
            m.ShowDialog();
        }
        public void moveUp(int aaa) 
        {
            ListItem _li = new ListItem(((ListItem)listBox1.SelectedItem).ID, ((ListItem)listBox1.SelectedItem).Name);
            int t = 0;
            int _t = listBox1.SelectedIndex;
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                if (((ListItem)listBox1.Items[i]).ID == aaa.ToString())
                {
                    t = i;
                }
            }
            listBox1.Items.RemoveAt(_t);
            listBox1.Items.Insert(t-1,_li);
            listBox1.SetSelected(t-1,true);
        }

        private void ToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            toolsDialog t = new toolsDialog();
            t.DbPath = txtDatabase.Text;
            t.ct = new toolsDialog.ChooseTools(ChooseTools);
            t.ShowDialog();
        }
        public void ChooseTools(int tools)
        {
            string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtDatabase.Text + ";Persist Security Info=False";
            OleDbConnection Conn = new OleDbConnection(ConnStr);
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = Conn;
            cmd.CommandText = "update yy_searchindex set toolsid="+ tools +" where id=" + ((ListItem)listBox1.SelectedItem).ID;
            Conn.Open();
            cmd.ExecuteNonQuery();
            Conn.Close();

        }
         

    }
    public class PhysicalComponent
    {
        public String GUID { get; set; }
        public String PhysicalComponentName { get; set; }
        public String isPartof { get; set; }
        public double Coordinate_X1 { get; set; }
        public double Coordinate_X2 { get; set; }
        public double Coordinate_Y1 { get; set; }
        public double Coordinate_Y2 { get; set; }
        public double Coordinate_Z1 { get; set; }
        public double Coordinate_Z2 { get; set; }

    }
    public class ListItem
    {
        private string id = string.Empty;
        private string name = string.Empty;
        public ListItem(string sid, string sname)
        {
            id = sid;
            name = sname;
        }
        public override string ToString()
        {
            return this.name;
        }
        public string ID
        {
            get
            {
                return this.id;
            }
            set
            {
                this.id = value;
            }
        }
        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }
    }
}

